{include file="public/head"/}

<body>
    <div class="layuimini-container">
        <div class="layuimini-main">

            <fieldset class="table-search-fieldset">
                <legend>搜索信息</legend>
                <div style="margin: 10px 10px 10px 10px">
                    <form class="layui-form layui-form-pane" action="">
                        <div class="layui-form-item">
                            <div class="layui-inline">
                                <label class="layui-form-label">标题查询</label>
                                <div class="layui-input-inline">
                                    <input type="text" name="title" autocomplete="off" class="layui-input">
                                </div>
                            </div>
                            <div class="layui-inline">
                                <button type="submit" class="layui-btn layui-btn-primary" lay-submit
                                    lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索</button>
                            </div>
                        </div>
                    </form>
                </div>
            </fieldset>

            <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add"> 添加 </button>
            </div>
        </script>

            <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>

            <script type="text/html" id="currentTableBar">
                <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="edit">编辑</a>
                <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete">删除</a>
            </script>

        </div>
    </div>
    <script type="text/html" id="status-templet-switch">
        <input type="checkbox" name="status" value="{{= d.id }}" title="启用中|已关闭" lay-skin="switch" lay-filter="demo-templet-status" {{= d.status == 1 ? "checked" : "" }}>
    </script>
    <script>
        layui.use(['form', 'table'], function () {
            var $ = layui.jquery,
                form = layui.form,
                table = layui.table;

            table.render({
                id: 'currentTableId',
                elem: '#currentTableId',
                url: '{:url("category")}',
                method: 'POST',
                toolbar: '#toolbarDemo',
                defaultToolbar: ['filter', 'exports', 'print', {
                    title: '提示',
                    layEvent: 'LAYTABLE_TIPS',
                    icon: 'layui-icon-tips'
                }],
                cols: [[
                    {field: 'id', title: '编号',  fixed: true},
                    {field: 'name', title: '文章名称',edit:'text'},
                    {field: 'status', title: '状态', align: 'center', width: 100, templet: '#status-templet-switch' },
                    { title: '操作', minWidth: 150, toolbar: '#currentTableBar', align: "center" }
                ]],
                limits: [10, 15, 20, 25, 50, 100],
                limit: 15,
                page: {
                    theme: '#1E9FFF'
                },
                skin: 'line'
            });

            //列表编辑
            table.on('edit(currentTableFilter)', function(obj){
                var data = obj.data;
                $.post('{:url("updateCate")}',{id:obj.data.id,field:obj.field,value:obj.value},function(res){
                    if(res.code==1){
                        layer.msg(res.msg,{time:1000,icon:1});
                    }else{
                        layer.msg(res.msg,{time:1000,icon:2});
                    }
                    $(".layui-laypage-btn")[0].click();
                });
            });

            // 监听搜索操作
            form.on('submit(data-search-btn)', function (data) {
                var data = data.field;
                //执行搜索重载
                table.reload('currentTableId', {
                    page: {
                        curr: 1,
                        theme: '#1E9FFF'
                    }
                    , where: {
                        key: data.title
                    }
                }, 'data');

                return false;
            });

            /**
             * toolbar监听事件
             */
            table.on('toolbar(currentTableFilter)', function (obj) {
                if (obj.event === 'add') {  // 监听添加操作
                    var index = layer.open({
                        title: '添加类型',
                        type: 2,
                        shade: 0.2,
                        maxmin: true,
                        shadeClose: true,
                        area: ['100%', '100%'],
                        content: '{:url("category_add")}',
                    });
                    $(window).on("resize", function () {
                        layer.full(index);
                    });
                }
            });

            //监听表格复选框选择
            table.on('checkbox(currentTableFilter)', function (obj) {
                console.log(obj)
            });

            table.on('tool(currentTableFilter)', function (obj) {
                var data = obj.data;
                if (obj.event === 'edit') {

                    var index = layer.open({
                        title: '编辑类型',
                        type: 2,
                        shade: 0.2,
                        maxmin: true,
                        shadeClose: true,
                        area: ['100%', '100%'],
                        content: '{:url("category_edit")}?id=' + data.id,
                    });
                    $(window).on("resize", function () {
                        layer.full(index);
                    });
                    return false;
                } else if (obj.event === 'delete') {
                    layer.confirm('真的删除类型么?', function (index) {
                        $.post('{:url("category_del")}',{id: data.id},function(res) {
                            obj.del();
                            layer.close(index);
                        }); 
                    });
                }
            });


            /**
              * 状态点击监听- 开关操作
            */
            form.on('switch(demo-templet-status)', function (obj) {
                var loading = layer.load(1, { shade: [0.1, '#fff'] });
                var id = this.value;
                $.post('{:url("category_state")}', { 'id': id }, function (res) {
                    layer.close(loading);
                    if (res.code > 0) {
                        tableIn.reload();
                    } else {
                        layer.msg('操作失败！', { time: 1000, icon: 2 });
                        return false;
                    }
                });

            });

        });
    </script>

    {include file="public/footer"/}